﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class PhieuTraPhongDAO
    {
        private SqlDataProvider dataProvider;

        public PhieuTraPhongDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<PhieuTraPhong> GetByAll(PhieuTraPhong obj)
        {
            List<PhieuTraPhong> lstData = new List<PhieuTraPhong>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_PhieuTraPhong_GetByAll"
                    , new string[] { "MaPhieuTraPhong", "PhieuThuePhong", "NgayTraPhong", "SoNgayThue", "GhiChu" }
                    , new object[] { obj.MaPhieuTraPhong, obj.PhieuThuePhongID, obj.NgayTraPhong, obj.SoNgayThue, obj.GhiChu }
                );
                PhieuTraPhong entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new PhieuTraPhong();
                    entity.MaPhieuTraPhong = Convert.ToInt32(myrow["MaPhieuTraPhong"]);
                    entity.NgayTraPhong = myrow["NgayTraPhong"] is DBNull ? string.Empty : myrow["NgayTraPhong"].ToString();
                    entity.PhieuThuePhongID = Convert.ToInt32(myrow["PhieuThuePhong"]);
                    entity.SoNgayThue = Convert.ToInt32(myrow["SoNgayThue"]);
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    try
                    {
                        if (entity.PhieuThuePhongID != null && entity.PhieuThuePhongID != 0)
                        {
                            entity.PhieuThuePhong = ((new PhieuThuePhongDAO()).GetByAll(new PhieuThuePhong(entity.PhieuThuePhongID))).FirstOrDefault();
                        }
                        else
                        {
                            entity.PhieuThuePhong = null;
                        }
                    }
                    catch
                    {
                        entity.PhieuThuePhong = null;
                    }

                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(PhieuTraPhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_PhieuTraPhong_Insert"
                    , new string[] { "MaPhieuTraPhong", "PhieuThuePhong", "NgayTraPhong", "SoNgayThue", "GhiChu" }
                    , new object[] { obj.MaPhieuTraPhong, obj.PhieuThuePhongID, obj.NgayTraPhong, obj.SoNgayThue, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Update(PhieuTraPhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_PhieuTraPhong_Update"
                    , new string[] { "MaPhieuTraPhong", "PhieuThuePhong", "NgayTraPhong", "SoNgayThue", "GhiChu" }
                    , new object[] { obj.MaPhieuTraPhong, obj.PhieuThuePhongID, obj.NgayTraPhong, obj.SoNgayThue, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(PhieuTraPhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_PhieuTraPhong_Delete"
                    , new string[] { "MaPhieuTraPhong", "PhieuThuePhong", "NgayTraPhong", "SoNgayThue", "GhiChu" }
                    , new object[] { obj.MaPhieuTraPhong, obj.PhieuThuePhongID, obj.NgayTraPhong, obj.SoNgayThue, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }
    }
}
